﻿using System;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;
using System.Collections.Generic;

namespace TTApp.Phone.Silverlight.Hexagons
{
    /// <summary>
    /// Thích phát âm thanh âm thiếc bơi hết vào đây
    /// </summary>
    public class SoundController
    {
        private bool isMute = false;
        private bool isPlayBackgroundMusic = false;

        Song backgoundSound;
        List<SoundEffect> listButtonClick = new List<SoundEffect>();
        List<SoundEffect> listEndGame = new List<SoundEffect>();
        List<SoundEffect> listHexagonJump = new List<SoundEffect>();
        List<SoundEffect> listHexagonSelect = new List<SoundEffect>();
        List<SoundEffect> listHexagonSpawn = new List<SoundEffect>();
        bool isBackgoundSoundFistTime = true;
        
        /// <summary>
        /// Load Toàn bộ Sound để chuẩn bị play()
        /// </summary>
        public SoundController()
        {
            try
            {
                isMute = false;

               // MediaPlayer.IsRepeating = true;

                backgoundSound = Song.FromUri("BackgoundMain", new Uri(@"Sounds/BackgroundSound/BackgoundMain.mp3", UriKind.Relative));

                listButtonClick.Add(SoundEffect.FromStream(TitleContainer.OpenStream(@"Sounds/ButtonClick/buttonClick.wav")));
                listButtonClick.Add(SoundEffect.FromStream(TitleContainer.OpenStream(@"Sounds/ButtonClick/buttonClickInvalid.wav")));

                listEndGame.Add(SoundEffect.FromStream(TitleContainer.OpenStream(@"Sounds/EndGame/FinalSound.wav")));

                listHexagonJump.Add(SoundEffect.FromStream(TitleContainer.OpenStream(@"Sounds/HexagonJump/HexagonJump1.wav")));
                listHexagonJump.Add(SoundEffect.FromStream(TitleContainer.OpenStream(@"Sounds/HexagonJump/HexagonJump2.wav")));

                listHexagonSelect.Add(SoundEffect.FromStream(TitleContainer.OpenStream(@"Sounds/HexagonSelect/tap.wav")));
                listHexagonSelect.Add(SoundEffect.FromStream(TitleContainer.OpenStream(@"Sounds/HexagonSelect/tap2.wav")));

                listHexagonSpawn.Add(SoundEffect.FromStream(TitleContainer.OpenStream(@"Sounds/HexagonSpawn/HexagonSpawn1.wav")));
                listHexagonSpawn.Add(SoundEffect.FromStream(TitleContainer.OpenStream(@"Sounds/HexagonSpawn/HexagonSpawn2.wav")));
            }
            catch (Exception ex)
            {
            }
        }

        public bool IsMute
        {
            set 
            { 
                isMute = value;
                if (isMute && isPlayBackgroundMusic)
                {
                    StopBackgroundSound();
                }
                if(!isMute && !isPlayBackgroundMusic)
                {
                    PlayBackgoundSound();
                }
            }
            get
            {
                return isMute;
            }
        }

        public void PlayBackgoundSound()
        {
            MediaPlayer.IsRepeating = true;
            isPlayBackgroundMusic = true;
            FrameworkDispatcher.Update();
            if (isBackgoundSoundFistTime)
            {
                MediaPlayer.Play(backgoundSound);
                isBackgoundSoundFistTime = false;
            }
            else
            {
                MediaPlayer.Resume();
            }
        }

        public void StopBackgroundSound()
        {
            FrameworkDispatcher.Update();
            MediaPlayer.Pause();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isInvalidClick">Biểu thị ấn đúng chỗ hay không đúng chỗ</param>
        public void PlayButtonClick(bool isValidClick)
        {
            if (isMute)
                return;
            FrameworkDispatcher.Update();
            if (isValidClick)
            {
                listButtonClick[0].Play();
            }
            else
            {
                listButtonClick[1].Play();
            }
        }

        public void PlayEndGame()
        {
            if (isMute)
                return;
            Random random = new Random();
            FrameworkDispatcher.Update();
            listEndGame[random.Next(listEndGame.Count)].Play();
        }

        public void PlayHexagonJump()
        {
            if (isMute)
                return;
            Random random = new Random();
            FrameworkDispatcher.Update();
            listHexagonJump[random.Next(listHexagonJump.Count)].Play();
        }

        public void PlayHexagonSelect()
        {
            if (isMute)
                return;
            Random random = new Random();
            FrameworkDispatcher.Update();
            listHexagonSelect[random.Next(listHexagonSelect.Count)].Play();
        }

        public void PlayHexagonSpawn()
        {
            if (isMute)
                return;
            Random random = new Random();
            FrameworkDispatcher.Update();
            listHexagonSpawn[random.Next(listHexagonSpawn.Count)].Play();
        }
    }
}
